## Create Table 3

.libPaths( c(.libPaths(), ""))

rm(list=ls())
setwd('')
if (!dir.exists('../2019output/movers'))   dir.create('../2019output/movers')
filepath = ''
library(data.table)
library(stargazer)
library(lfe)

# change to geo/ein 
VERSION = 'ein'


### DATA PREP ###

# Load movers data 
dt = data.table(fread(paste0('../2019data/', VERSION, '_movers.csv')))
if (VERSION == 'geo')    dt = dt[, ':='(orgn_loc = lag_geo, dest_loc = geo)]   
if (VERSION == 'ein')    dt = dt[, ':='(orgn_loc = lag_ein, dest_loc = ein)]
dt = dt[,.(SSN, move_age, orgn_loc, dest_loc, sex, race, dob, retire_age = retire1, claim_age)]


# Pull "stickiness" of lagged location 
stick = data.table(fread(paste0('../2019data/', VERSION, '_avg.csv')))     # load averages
if (VERSION == 'geo')  stick = stick[, loc := geo_60]
if (VERSION == 'ein')  stick = stick[, loc := ein_60]
stick = stick[, .(loc, new_fra = ifelse(new_fra == 1, 'post', 'pre'),
                  retire_65 = mean_retire_age_65, retire_66 = mean_retire_age_66,
                  claim_65 = mean_claim_age_65, claim_66 = mean_claim_age_66)]
stick = dcast(stick, loc ~ new_fra, value.var = c('retire_65', 'retire_66', 'claim_65', 'claim_66'))

# Merge origin and destination 
orgn = copy(stick)
setnames(orgn, old = names(orgn), new = paste0('orgn_', names(orgn)))
dt = merge(dt, orgn, by = 'orgn_loc', all_x = T)

dest = copy(stick)
setnames(dest, old = names(dest), new = paste0('dest_', names(dest)))
dt = merge(dt, dest, by = 'dest_loc', all_x = T)

dt = dt[, ':='(	retire_65 = as.numeric(retire_age == 65), 
			retire_66 = as.numeric(retire_age == 66), 
			claim_65 = as.numeric(claim_age >= 65 & claim_age < 66),
			claim_66 = as.numeric(claim_age >= 66 & claim_age < 67),
			delt_orgn_claim_65 = ((orgn_claim_65_post - orgn_claim_65_pre)),
			delt_orgn_claim_66 = ((orgn_claim_66_post - orgn_claim_66_pre)),
			delt_orgn_retire_65 = ((orgn_retire_65_post - orgn_retire_65_pre)),
			delt_orgn_retire_66 = ((orgn_retire_66_post - orgn_retire_66_pre)),
			delt_dest_claim_65 = ((dest_claim_65_post - dest_claim_65_pre)),
			delt_dest_claim_66 = ((dest_claim_66_post - dest_claim_66_pre)),
			delt_dest_retire_65 = ((dest_retire_65_post - dest_retire_65_pre)),
			delt_dest_retire_66 = ((dest_retire_66_post - dest_retire_66_pre))
			)]
summary(dt)

dt = dt[, ':='(dob_yy = substr(dob,6,9))]
dtpost = dt[dob_yy >= 1943,]
dtpre  = dt[dob_yy <  1943,]
summary(dtpost)
summary(dtpre)


### MOVERS REGRESSIONS ###

for (stub in c("retire","claim")) {
	for (age in c("65","66")) {

	eval(parse(text = paste0("reg1_",stub,"_",age,"_prepost = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre | orgn_loc | 0 | orgn_loc, data = dt)")))

	eval(parse(text = paste0("reg2_",stub,"_",age,"_prepost = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre + delt_dest_",stub,"_",age," | orgn_loc | 0 | orgn_loc, data = dt)")))

	eval(parse(text = paste0("reg3_",stub,"_",age,"_prepost = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre | 0 | 0 | orgn_loc, data = dt)")))

	eval(parse(text = paste0("reg4_",stub,"_",age,"_prepost = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre + delt_orgn_",stub,"_",age," + delt_dest_",stub,"_",age," | 0 | 0 | orgn_loc, data = dt)")))


	eval(parse(text = paste0("reg5_",stub,"_",age,"_post = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre | orgn_loc | 0 | orgn_loc, data = dtpost)")))

	eval(parse(text = paste0("reg6_",stub,"_",age,"_post = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre + delt_dest_",stub,"_",age," | orgn_loc | 0 | orgn_loc, data = dtpost)")))

	eval(parse(text = paste0("reg7_",stub,"_",age,"_post = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre | 0 | 0 | orgn_loc, data = dtpost)")))

	eval(parse(text = paste0("reg8_",stub,"_",age,"_post = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre + delt_orgn_",stub,"_",age," + delt_dest_",stub,"_",age," | 0 | 0 | orgn_loc, data = dtpost)")))



	eval(parse(text = paste0("reg9_",stub,"_",age,"_pre = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre | orgn_loc | 0 | orgn_loc, data = dtpre)")))

	eval(parse(text = paste0("reg10_",stub,"_",age,"_pre = felm(",stub,"_",age," ~ dest_",stub,"_",age,"_pre + delt_dest_",stub,"_",age," | orgn_loc | 0 | orgn_loc, data = dtpre)")))

	eval(parse(text = paste0("reg11_",stub,"_",age,"_pre = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre | 0 | 0 | orgn_loc, data = dtpre)")))

	eval(parse(text = paste0("reg12_",stub,"_",age,"_pre = felm(",stub,"_",age," ~ orgn_",stub,"_",age,"_pre + dest_",stub,"_",age,"_pre + delt_orgn_",stub,"_",age," + delt_dest_",stub,"_",age," | 0 | 0 | orgn_loc, data = dtpre)")))



	eval(parse(text = paste0("stargazer(reg1_",stub,"_",age,"_prepost, reg2_",stub,"_",age,"_prepost, reg3_",stub,"_",age,"_prepost, reg4_",stub,"_",age,"_prepost,
							reg5_",stub,"_",age,"_post, reg6_",stub,"_",age,"_post, reg7_",stub,"_",age,"_post, reg8_",stub,"_",age,"_post,
							reg9_",stub,"_",age,"_pre, reg10_",stub,"_",age,"_pre, reg11_",stub,"_",age,"_pre, reg12_",stub,"_",age,"_pre,

					

          	title = '",stub,"',  keep.stat = c('n','rsq'),
          	type = 'text', 
		out = paste0(filepath, '/Table3_",VERSION,"_",stub,"_",age,".txt'))")))
	}

}



